<?php
class CourseController extends AdminBaseController {
	public function lists (){
		$employees = HttpServerManager::getCourseServer();
		$data = $employees->pagingBySearch($_GET);

		$level_list = HttpDaoManager::getLevelDao()->searchList();
		$pair = array();
		foreach($level_list as $k=>$v){
			if(isset($v['id'])){
				//$pair[$v['id']]=$v['level'].'-'.$v['subject'];
				$pair[$v['id']]='Level'.'-'.$v['level'];
			}
		}	

		unset($level_list);
		$data['english_type_pair'] = Common::_t('english_type');				
		$data['course_type'] =Common::_t('course_type');
		$data['status'] = Common::_t('course_status');
		$data['book_press_pair']=Common::_t('book_press');
		$data['level'] = $pair;
		
		return $data;
	}
	
	public function add(){
		$data = array();
		if (!empty($_GET['id'])){
			$data = HttpDaoManager::getCourseDao()->get($_GET['id']);
		}else{
			$sysKey = 'COURSE_NO';
			$key_info  =  HttpDaoManager::getSysConfigDao()->getBySyskey($sysKey);
			$seed_no = $key_info['sys_value'];	
			$data['serial_number'] = Common::executeSerialNumber('C','7',$seed_no);
			$sys_update = array();
			$sys_update['sys_value']=$seed_no+1;
			$sys_id = $key_info['id'];
			$key_info  =  HttpDaoManager::getSysConfigDao()->update($sys_update,$sys_id);			
		}
		$teaching_material = Common::getStrValue($data,'teaching_material');
		//$data['teaching_material'] = HttpServerManager::getUploadServer()->eosUploadShow('teaching_material', $teaching_material);
		
		$data['course_types'] = Common::_t('course_type');
		$data['english_type_pair'] = Common::_t('english_type');
// 		$data['big_course_type_pair'] = Common::_t('big_course_type');
		$data['book_press_pair']=Common::_t('book_press');
		
		$data['status'] = isset($data['status'])?$data['status']:'';
		$data['level'] = isset($data['level'])?$data['level']:'';
		
		 
		$data['departmentList'] = Common::_t('department');;
		$employees = array();
		$employeesDao = HttpDaoManager::getEmployeesDao();
		foreach ($data['departmentList'] as $k=>$v){
			$employees[$k] = $employeesDao->getListByDepartment($k);
		}
		$data['homemade_producer_list'] = $employees;

		$status = Common::_t('course_status');

		$level_list = HttpDaoManager::getLevelDao()->searchList();
		$level_pair = array();
		foreach($level_list as $k=>$v){
			if(isset($v['id'])){
				//$level_pair[$v['id']]='Level'.'-'.$v['level'].':'.$v['subject'];
				$level_pair[$v['id']]='Level'.'-'.$v['level'];
			}
		}		
		unset($level_list);
		
		$purpose_list = HttpDaoManager::getPurposeDao()->getListByFatherId(0);
		$for_purpose_study_pair = array();
		foreach($purpose_list as $k=>$v){
			$for_purpose_study_pair[$v['id']] = $v['title'];
		}
		
		$data['for_purpose_study_pair'] = $for_purpose_study_pair;
		
		$purpose_list = HttpDaoManager::getPurposeDao()->getAllChildList();
		$for_purpose_people_pair = array();
		foreach($purpose_list as $k=>$v){
			$for_purpose_people_pair[$v['id']] = $v['title'];
		}
		
		$data['for_purpose_people_pair'] = $for_purpose_people_pair;
		
		$data['levelOptions'] = page_modifier_options($level_pair,common::getStrValue($data, 'level'));
		$data['statusOptions'] =  page_modifier_options($status,$data['status']);

		$ueditor = ClassManager::getUeditor();
		$overview = Common::getStrValue($data, 'overview');
		$data['ueditor_overview'] = $ueditor->ueditor().$ueditor->ueditors_2('overview',$overview);

		$describe = Common::getStrValue($data, 'describe');
		$data['ueditor_describe'] = $ueditor->ueditors_2('describe',$describe);

		
		$data['id'] = isset($data['id'])?$data['id']: Common::getGetStrValue('id');		
		return $data;
	}
	public function subAdd(){
		 $tmp = HttpServerManager::getCourseServer()->add($_POST);
		 if ($tmp === true){
		 	ClassManager::getToos()->msgbox(Common::_t('data_insert_success'));
		 }
		 else{
		 	$msg = Common::_t('course_add_error');
		 	ClassManager::getToos()->msgbox($msg[$tmp]);
		 }
		
	}
	public function subUpdate(){
		HttpServerManager::getCourseServer()->update($_POST, $_POST['id']);
		ClassManager::getToos()->msgbox(Common::_t('data_edit_success'),'course_lists.html');
	}
	public function delete(){
		HttpServerManager::getCourseServer()->deleteBatchByid($_GET['id']);
		ClassManager::getToos()->msgbox(Common::_t('data_del_success'));
	}
	public function download(){
		$data = HttpDaoManager::getCourseDao()->get($_GET['id']);
		if(!empty($data)){
			$tmp = $data['teaching_material'];
			$new_file_name="";
			if($tmp!=""){
				$pos= strrpos($tmp,'/');
				$file_head = substr($tmp, 0,$pos+1);
				$file_name = substr($tmp, $pos+1);
				$file_name = preg_replace('/\s{2, }| /','%20',$file_name);
				$new_file_name = $file_head.$file_name;
			}
			ClassManager::getToos()->download(SITE_URL.$new_file_name);
			exit ;
		}
		exit ;
	}
}
?>